import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'index.dart';

class WelcomePage extends GetView<WelcomeController> {
  const WelcomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final state = controller.state;
    final _list = state.list ?? [];
    return GetBuilder<WelcomeController>(
      builder: (_) {
        return Scaffold(
          body: _list.isEmpty
              ? null
              : Stack(
                  children: [
                    PageView.builder(
                      controller: state.pageCtrl,
                      itemCount: _list.length,
                      itemBuilder: (_, index) {
                        return Image.network(
                          _list[index].url!,
                          width: Get.width,
                          height: Get.height,
                          fit: BoxFit.cover,
                        );
                      },
                      onPageChanged: (value) {
                        state.currentIndex.value = value;
                      },
                    ),
                    Obx(
                      () => Positioned(
                        bottom: 30.h,
                        width: Get.width,
                        child: SafeArea(
                          child: state.currentIndex.value == _list.length - 1
                              ? Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    InkWell(
                                      onTap: controller.toMain,
                                      child: Container(
                                        width: 60.r,
                                        height: 60.r,
                                        alignment: Alignment.center,
                                        decoration: BoxDecoration(
                                          color: Colors.red[400],
                                          borderRadius:
                                              BorderRadius.circular(30.r),
                                        ),
                                        child: Text(
                                          '→',
                                          style: TextStyle(
                                              color: Colors.white,
                                              fontSize: 26.sp,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ),
                                    ),
                                  ],
                                )
                              : Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: _list
                                      .asMap()
                                      .map(
                                        (index, e) => MapEntry(
                                          index,
                                          Container(
                                            width: 8.r,
                                            height: 8.r,
                                            margin: const EdgeInsets.symmetric(
                                                horizontal: 3),
                                            decoration: BoxDecoration(
                                                color: index ==
                                                        state.currentIndex.value
                                                    ? Colors.white
                                                    : const Color(0x66ffffff),
                                                borderRadius:
                                                    BorderRadius.circular(4.r)),
                                          ),
                                        ),
                                      )
                                      .values
                                      .toList(),
                                ),
                        ),
                      ),
                    ),
                  ],
                ),
        );
      },
    );
  }
}
